package com.icode.service.impl.user;

import com.icode.core.dto.house.RentDTO;
import com.icode.core.dto.manager.ContactsDTO;
import com.icode.core.dto.manager.MessageModelDTO;
import com.icode.core.dto.order.OrderDTO;
import com.icode.core.model.manage.Contacts;
import com.icode.core.model.manage.MessageModel;
import com.icode.core.model.manage.ModelTemplate;
import com.icode.core.model.manage.Reserve;
import com.icode.core.shared.ValidateNumHandle;
import com.icode.dao.MessageDao;
import com.icode.service.user.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xianfei
 * Date: 15-6-4
 * Time: 下午3:18
 * To change this template use File | Settings | File Templates.
 */
@Transactional
@Service("messageService")
@PropertySource("classpath:note.properties")
public class MessageServiceImpl implements MessageService {

    @Autowired
    private Environment env;
    @Autowired
    private MessageDao messageDao;

    @Override
    @Transactional
    public String sendValidateNum(String phoneNum) {
        String message = null;
        try {
            message = new String(env.getProperty("message.register").getBytes("iso-8859-1"), "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return ValidateNumHandle.sendValidateNum(message, phoneNum);
    }

    @Override
    @Transactional
    public void removePhoneNum(String phoneNum) {
        ValidateNumHandle.removePhoneNum(phoneNum);
    }

    @Override
    @Transactional
    public String sendToCustomer(String phoneNum, String custProduct, String custPaid, String custSurplus, String custAddress) {
        String message = null;
        try {
            message = new String(env.getProperty("message.customer").getBytes("iso-8859-1"), "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        message = message.replaceAll("\\{0\\}", custProduct);
        message = message.replaceAll("\\{1\\}", custPaid);
        message = message.replaceAll("\\{2\\}", custSurplus);
        message = message.replaceAll("\\{3\\}", custAddress);
        return ValidateNumHandle.sendToCustomerOrService(message, phoneNum);
    }

    @Override
    @Transactional
    public String sendToService(String phoneNum, String serviceCustomerName, String gender, String serviceProduct, String servicePaid, String serviceCustomerTel) {
        String message = null;
        try {
            message = new String(env.getProperty("message.service").getBytes("iso-8859-1"), "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        message = message.replaceAll("\\{0\\}", serviceCustomerName);
        message = message.replaceAll("\\{1\\}", gender);
        message = message.replaceAll("\\{2\\}", serviceProduct);
        message = message.replaceAll("\\{3\\}", servicePaid);
        message = message.replaceAll("\\{4\\}", serviceCustomerName);
        message = message.replaceAll("\\{5\\}", gender);
        message = message.replaceAll("\\{6\\}", serviceCustomerTel);
        return ValidateNumHandle.sendToCustomerOrService(message, phoneNum);
    }

    @Override
    @Transactional
    public void saveOrUpdateContacts(ContactsDTO contactsDTO) {
        if ("".equals(contactsDTO.getGuid())) {
            Contacts contacts = ContactsDTO.toContacts(contactsDTO);
            messageDao.saveOrUpdate(contacts);
        } else {
            Contacts contacts = messageDao.findByGuid(Contacts.class, contactsDTO.getGuid());
            contacts.initContacts(contactsDTO);
        }
    }

    @Override
    @Transactional
    public ContactsDTO findContactsByGuid(String contactsGuid) {
        Contacts contacts = messageDao.findByGuid(Contacts.class, contactsGuid);
        return ContactsDTO.toDTO(contacts);
    }

    @Override
    @Transactional
    public List<ContactsDTO> findContacts(String currentPage) {
        List<Contacts> contactses = messageDao.findContacts(currentPage);
        List<ContactsDTO> contactsDTOs = ContactsDTO.toDTOs(contactses);
        return contactsDTOs;
    }

    @Override
    @Transactional
    public void delContactsByGuid(String contactsGuid) {
        Contacts contacts = messageDao.findByGuid(Contacts.class, contactsGuid);
        messageDao.delContacts(contacts);
    }

    @Override
    @Transactional
    public void delmoreContacts(String contactsGuids) {
        messageDao.delmoreContacts(contactsGuids.split(","));
    }

    @Override
    @Transactional
    public boolean sendModel(String guids, String modelGuid, String modelValue, String address, String signTime, String signAddress, String signContact, String signPhone) {
        boolean result = true;
        try {
            MessageModel messageModel = messageDao.findByGuid(MessageModel.class, modelGuid);
            String[] guid = guids.split(",");
            String message;
            for (int i = 0; i < guid.length; i++) {
                Contacts contacts = messageDao.findByGuid(Contacts.class, guid[i]);
                if ("CUSTOMER1".equals(messageModel.name())) {
                    message = new String(env.getProperty("message.customer.first").getBytes("iso-8859-1"), "utf-8");
                    message = message.replaceAll("\\{0\\}", contacts.product() + contacts.week());
                    message = message.replaceAll("\\{1\\}", contacts.paid() + " 元");
                    message = message.replaceAll("\\{2\\}", address);
                } else if ("CUSTOMER2".equals(messageModel.name())) {
                    message = new String(env.getProperty("message.customer.second").getBytes("iso-8859-1"), "utf-8");
                    message = message.replaceAll("\\{0\\}", contacts.product() + contacts.week());
                    message = message.replaceAll("\\{1\\}", contacts.paid() + " 元");
                    message = message.replaceAll("\\{2\\}", contacts.remainder() + " 元");
                    message = message.replaceAll("\\{3\\}", address);
                } else if ("NOTIFY".equals(messageModel.name())) {
                    message = new String(env.getProperty("message.customer.notice").getBytes("iso-8859-1"), "utf-8");
                } else {
                    message = new String(env.getProperty("message.customer.third").getBytes("iso-8859-1"), "utf-8");
                    message = message.replaceAll("\\{0\\}", contacts.product() + contacts.week());
                    message = message.replaceAll("\\{1\\}", contacts.price() + " 元");
                    message = message.replaceAll("\\{2\\}", signTime.substring(0, 4));
                    message = message.replaceAll("\\{3\\}", signTime.substring(5, 7));
                    message = message.replaceAll("\\{4\\}", signTime.substring(8, 10));
                    message = message.replaceAll("\\{5\\}", signTime.substring(11, 13));
                    message = message.replaceAll("\\{6\\}", signAddress);
                    message = message.replaceAll("\\{7\\}", signContact);
                    message = message.replaceAll("\\{8\\}", signPhone);
                }
                ValidateNumHandle.sendToCustomerOrService(message, contacts.phone());
            }
        } catch (Exception e) {
            result = false;
        }
        return result;
    }

    @Override
    @Transactional
    public List<MessageModelDTO> findMessageModelByName(ModelTemplate template) {
        List<MessageModel> messageModels = messageDao.findMessageModelByName(template);
        List<MessageModelDTO> messageModelDTOs = MessageModelDTO.toDTOs(messageModels);
        return messageModelDTOs;
    }

    @Override
    @Transactional
    public List<MessageModelDTO> findAllMessageModel() {
        List<MessageModel> messageModels = messageDao.findAllMessageModel();
        List<MessageModelDTO> messageModelDTOs = MessageModelDTO.toDTOs(messageModels);
        return messageModelDTOs;
    }

    @Override
    @Transactional
    public boolean messageModelSaveOrUpdate(MessageModelDTO messageModelDTO) {
        boolean result = true;
        try {
            if ("".equals(messageModelDTO.getGuid())) {
                MessageModel messageModel = MessageModelDTO.toMessageModel(messageModelDTO);
                messageDao.saveOrUpdate(messageModel);
            } else {
                MessageModel messageModel = messageDao.findByGuid(MessageModel.class, messageModelDTO.getGuid());
                messageModel.initMessageModel(messageModelDTO.getName(), messageModelDTO.getValue());
            }
        } catch (Exception e) {
            result = false;
        }
        return result;
    }

    @Override
    @Transactional
    public boolean messageModelDelete(String modelGuid) {
        boolean result = true;
        try {
            if (!"".equals(modelGuid)) {
                MessageModel messageModel = messageDao.findByGuid(MessageModel.class, modelGuid);
                messageDao.deleteObject(messageModel);
            }
        } catch (Exception e) {
            result = false;
        }
        return result;
    }

    @Override
    @Transactional
    public MessageModelDTO findMessageModelByGuid(String modelGuid) {
        MessageModel messageModel = messageDao.findByGuid(MessageModel.class, modelGuid);
        MessageModelDTO messageModelDTO = MessageModelDTO.toDTO(messageModel);
        return messageModelDTO;
    }

    @Override
    @Transactional
    public void sendPayment(OrderDTO orderDTO) {
        String message = null;
        try {
            message = new String(env.getProperty("message.customer.payment").getBytes("iso-8859-1"), "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        message = message.replaceAll("\\{0\\}", orderDTO.getHouseInfoDTO().getName());
        message = message.replaceAll("\\{1\\}", orderDTO.getHouseWeekDTO().getName());
        message = message.replaceAll("\\{2\\}", orderDTO.getOrderInfoDTO().getTotalPrice());
        ValidateNumHandle.sendToCustomerOrService(message, orderDTO.getPhoneNum());
    }

    @Override
    @Transactional
    public void sendReserveSuccess(Reserve reserve) {
        String message = null;
        try {
            message = new String(env.getProperty("message.customer.reservesuccess").getBytes("iso-8859-1"), "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        message = message.replaceAll("\\{0\\}", reserve.houseInfo().baseInfo().houseName());
        message = message.replaceAll("\\{1\\}", reserve.houseWeek().name());
        message = message.replaceAll("\\{2\\}", reserve.houseWeek().price());
        ValidateNumHandle.sendToCustomerOrService(message, reserve.phoneNum());
    }

    @Override
    @Transactional
    public void sendRentSuccess(RentDTO rentDTO) {
        String message = null;
        try {
            message = new String(env.getProperty("message.customer.rentsuccess").getBytes("iso-8859-1"), "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        message = message.replaceAll("\\{0\\}", rentDTO.getHouseName());
        message = message.replaceAll("\\{1\\}", rentDTO.getHouseWeek());
        message = message.replaceAll("\\{2\\}", rentDTO.getPrice());
        ValidateNumHandle.sendToCustomerOrService(message, rentDTO.getOwnerPhone());
    }
}
